package com.zenithmind.document.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.Min;
import java.time.LocalDateTime;

/**
 * 文档统计信息值对象
 * 遵循单一职责原则：专注于文档统计数据管理
 * 遵循封装原则：通过方法控制统计数据的更新
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@TableName("document_statistics")
@Schema(description = "文档统计信息")
public class DocumentStatistics extends BaseEntity {

    /**
     * 文档ID
     */
    @TableField("document_id")
    @Schema(description = "文档ID")
    private String documentId;

    /**
     * 下载次数
     */
    @Min(value = 0, message = "下载次数不能为负数")
    @TableField("download_count")
    @Schema(description = "下载次数")
    private Long downloadCount = 0L;

    /**
     * 查看次数
     */
    @Min(value = 0, message = "查看次数不能为负数")
    @TableField("view_count")
    @Schema(description = "查看次数")
    private Long viewCount = 0L;

    /**
     * 收藏次数
     */
    @Min(value = 0, message = "收藏次数不能为负数")
    @TableField("favorite_count")
    @Schema(description = "收藏次数")
    private Long favoriteCount = 0L;

    /**
     * 评分
     */
    @TableField("rating")
    @Schema(description = "评分")
    private Double rating = 0.0;

    /**
     * 评分次数
     */
    @Min(value = 0, message = "评分次数不能为负数")
    @TableField("rating_count")
    @Schema(description = "评分次数")
    private Long ratingCount = 0L;

    /**
     * 分享次数
     */
    @Min(value = 0, message = "分享次数不能为负数")
    @TableField("share_count")
    @Schema(description = "分享次数")
    private Long shareCount = 0L;

    /**
     * 最后访问时间
     */
    @TableField("last_access_time")
    @Schema(description = "最后访问时间")
    private LocalDateTime lastAccessTime;

    /**
     * 最后下载时间
     */
    @TableField("last_download_time")
    @Schema(description = "最后下载时间")
    private LocalDateTime lastDownloadTime;

    // ========== 业务方法 ==========

    /**
     * 创建文档统计信息
     */
    public static DocumentStatistics create(String documentId) {
        DocumentStatistics statistics = new DocumentStatistics();
        statistics.setDocumentId(documentId);
        return statistics;
    }

    /**
     * 设置文档ID
     */
    public void setDocumentId(String documentId) {
        if (documentId == null || documentId.trim().isEmpty()) {
            throw new IllegalArgumentException("文档ID不能为空");
        }
        this.documentId = documentId;
    }

    /**
     * 增加下载次数
     */
    public void incrementDownloadCount() {
        this.downloadCount++;
        this.lastDownloadTime = LocalDateTime.now();
    }

    /**
     * 增加查看次数
     */
    public void incrementViewCount() {
        this.viewCount++;
        this.lastAccessTime = LocalDateTime.now();
    }

    /**
     * 增加收藏次数
     */
    public void incrementFavoriteCount() {
        this.favoriteCount++;
    }

    /**
     * 减少收藏次数
     */
    public void decrementFavoriteCount() {
        if (this.favoriteCount > 0) {
            this.favoriteCount--;
        }
    }

    /**
     * 增加分享次数
     */
    public void incrementShareCount() {
        this.shareCount++;
    }

    /**
     * 更新评分
     */
    public void updateRating(Double newRating) {
        if (newRating == null || newRating < 0 || newRating > 5) {
            throw new IllegalArgumentException("评分必须在0-5之间");
        }
        
        // 计算新的平均评分
        double totalRating = (this.rating * this.ratingCount) + newRating;
        this.ratingCount++;
        this.rating = totalRating / this.ratingCount;
    }

    /**
     * 获取总访问次数
     */
    public Long getTotalAccessCount() {
        return this.viewCount + this.downloadCount;
    }

    /**
     * 检查是否为热门文档（基于访问次数）
     */
    public boolean isPopular(Long threshold) {
        return getTotalAccessCount() >= (threshold != null ? threshold : 100L);
    }

    /**
     * 检查是否为高评分文档
     */
    public boolean isHighRated() {
        return this.rating >= 4.0 && this.ratingCount >= 5;
    }

    /**
     * 获取评分星级描述
     */
    public String getRatingDescription() {
        if (this.rating >= 4.5) return "优秀";
        if (this.rating >= 4.0) return "良好";
        if (this.rating >= 3.0) return "一般";
        if (this.rating >= 2.0) return "较差";
        return "很差";
    }

    /**
     * 重置统计数据
     */
    public void reset() {
        this.downloadCount = 0L;
        this.viewCount = 0L;
        this.favoriteCount = 0L;
        this.shareCount = 0L;
        this.rating = 0.0;
        this.ratingCount = 0L;
        this.lastAccessTime = null;
        this.lastDownloadTime = null;
    }

    /**
     * 检查统计数据是否有效
     */
    public boolean isValid() {
        return documentId != null && !documentId.trim().isEmpty() &&
               downloadCount >= 0 && viewCount >= 0 && favoriteCount >= 0 &&
               shareCount >= 0 && ratingCount >= 0 &&
               rating >= 0.0 && rating <= 5.0;
    }
}
